﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Steamrunner
{
    public class ParallaxScrollning : DrawableGameComponent
    {
        SpriteBatch spritebatch;

        Vector2 position;
        private Vector2 direction = new Vector2(-1, 0);
        float speed;

        string filename;

        Texture2D backgroundTexture;

        public ParallaxScrollning(Game game, Vector2 position, string filename, float speed, SpriteBatch spriteBatch)
            : base(game)
        {
            this.position = position;
            this.filename = filename;
            this.speed = speed;
            this.spritebatch = spriteBatch;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            backgroundTexture = Game.Content.Load<Texture2D>(filename);

            base.LoadContent();
        }


        public override void Update(GameTime gameTime)
        {
            if (position.X < (0 - backgroundTexture.Width))
            {
                position.X = position.X + (backgroundTexture.Width * 1);
            }

            position += direction * speed * gameTime.ElapsedGameTime.Milliseconds;
        }

        public override void Draw(GameTime gameTime)
        {
            spritebatch.Draw(backgroundTexture, position, Color.White);
            spritebatch.Draw(backgroundTexture, new Vector2(position.X + backgroundTexture.Width, position.Y), Color.White);
            spritebatch.Draw(backgroundTexture, new Vector2(position.X + (backgroundTexture.Width * 2), position.Y), Color.White);

            base.Draw(gameTime);
        }
    }
}
